package com.rocpenghua.ui;

import com.rocpenghua.config.PluginSettings;
import com.intellij.openapi.options.Configurable;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class SettingsPanel implements Configurable {
    private JPanel panel;
    private JTextField apiKeyField;
    private JTextField baseUrlField;
    private JComboBox<String> modelComboBox;

    @Override
    public String getDisplayName() {
        return "AI Code Assistant";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        apiKeyField = new JTextField(40);
        baseUrlField = new JTextField(40);
        modelComboBox = new JComboBox<>(new String[]{"deepseek-v3", "deepseek-r1"});

        PluginSettings settings = PluginSettings.getInstance();
        apiKeyField.setText(settings.getApiKey());
        baseUrlField.setText(settings.getBaseUrl());
        modelComboBox.setSelectedItem(settings.getSelectedModel());

        panel.add(new JLabel("API Key:"));
        panel.add(apiKeyField);
        panel.add(new JLabel("Base URL:"));
        panel.add(baseUrlField);
        panel.add(new JLabel("AI Model:"));
        panel.add(modelComboBox);

        return panel;
    }

    @Override
    public boolean isModified() {
        PluginSettings settings = PluginSettings.getInstance();
        return !apiKeyField.getText().equals(settings.getApiKey()) ||
                !baseUrlField.getText().equals(settings.getBaseUrl()) ||
                !modelComboBox.getSelectedItem().equals(settings.getSelectedModel());
    }

    @Override
    public void apply() {
        PluginSettings settings = PluginSettings.getInstance();
        settings.setApiKey(apiKeyField.getText());
        settings.setBaseUrl(baseUrlField.getText());
        settings.setSelectedModel((String) modelComboBox.getSelectedItem());
    }

    @Override
    public void reset() {
        PluginSettings settings = PluginSettings.getInstance();
        apiKeyField.setText(settings.getApiKey());
        baseUrlField.setText(settings.getBaseUrl());
        modelComboBox.setSelectedItem(settings.getSelectedModel());
    }

    @Override
    public void disposeUIResources() {
        panel = null;
    }
}